﻿using HandyControl.Controls;
using Power.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Power.View
{
    /// <summary>
    /// TestRecord.xaml 的交互逻辑
    /// </summary>
    public partial class TestRecord : UserControl
    {
        public TestRecordViewModel Context { get; set; }
        public TestRecord()
        {
            InitializeComponent();
            Context = new TestRecordViewModel(this);
            this.DataContext = Context;
        }

        private void selectPopup_Closed(object sender, EventArgs e)
        {
            foreach (var item in this.Context.ColumnList)
            {
                var index = this.Context.ColumnList.IndexOf(item);
                if (index == 0)
                {
                    continue;
                }
                this.grid.Columns[index].Visibility = item.IsChecked ? Visibility.Visible : Visibility.Collapsed;
            }
        }

        private void grid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            DataGrid dg = sender as DataGrid;
            Point point = e.GetPosition(dg);
            IInputElement obj = dg.InputHitTest(point);

            if (obj is TextBlock)
            {
                var columnName = (obj as TextBlock).Text;
                var list = dg.Columns.Where(c => c.Header?.ToString() == columnName);
                //var list = this.Context.Data.ColumnList.Select(p => p.ColumnName == columnName);
                if (list != null)
                {
                    foreach (var item in list)
                    {
                        this.Context.ColumnList[item.DisplayIndex].IsChecked = false;
                        dg.Columns[item.DisplayIndex].Visibility = Visibility.Hidden;

                    }
                }
            }
        }

        private void ScrollViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
        {
            var eventArg = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta);
            eventArg.RoutedEvent = UIElement.MouseWheelEvent;
            eventArg.Source = sender;
            scro1.RaiseEvent(eventArg);
        }
    }
}
